from universal.common_header import *
from mylibs.lib import addActions,generateColorByText,struct
from Module.xRayDetection.xray.logs import Logger

class Listwidget(QListWidget):
    def __init__(self,parent=None):
        super(Listwidget, self).__init__(parent=parent)
        self.mainwindow = parent

    def Listwidget_color(self):
        self.setStyleSheet("QListWidget::item:selected{\n""  background-color: rgb(189, 147, 249);\n""}")
        self.itemDoubleClicked.connect(self.fileitemDoubleClicked)
        self.setSpacing(1)

    def fileitemDoubleClicked(self, item=None):  # 双击显示
        currIndex = self.currentRow()
        # currentItem = self.currentItem()
        # xuhao = currentItem.text().split("—>")[0]
        # index = xuhao.split("/")[0].split("第")[1]
        # currIndex = int(index)
        if int(currIndex) < len(self.mainwindow.compressPicPath_):
            filename = self.mainwindow.compressPicPath_[int(currIndex)]
            if filename:
                self.mainwindow.loadFile(filename)


    def CLASS_(self):
        labelMenu = QMenu()
        if self.mainwindow.projectTask == '目标检测' or self.mainwindow.projectTask == '实例分割':
            addActions(labelMenu, (self.mainwindow.edit, self.mainwindow.delete))
            self.itemActivated.connect(self.labelSelectionChanged)
            self.itemSelectionChanged.connect(self.labelSelectionChanged)
            self.itemDoubleClicked.connect(self.editLabel)
            self.itemChanged.connect(self.labelItemChanged)

        if self.mainwindow.projectTask == '图像分类':
            addActions(labelMenu, (self.mainwindow.actionTool.Add_, self.mainwindow.actionTool.edit222, self.mainwindow.actionTool.Delete_))
            self.itemDoubleClicked.connect( lambda: self.mainwindow.xrayClass_func(str(self.currentItem().text())))

        self.menus1 = struct(labelList=labelMenu)  #
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.popLabelListMenu)

    def popLabelListMenu(self, point):
        self.menus1.labelList.exec_(self.mapToGlobal(point))  #

    def labelItemChanged(self, item):
        if self.mainwindow.projectTask == '目标检测' or self.mainwindow.projectTask == '实例分割':
            shape = self.mainwindow.itemsToShapes[item]
            label = item.text()
            if label != shape.label:
                shape.label = item.text()
                shape.line_color = generateColorByText(shape.label)
                self.mainwindow.setDirty()
            else:  # User probably changed item visibility
                self.mainwindow.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked)

    def labelSelectionChanged(self):
        if self.mainwindow.projectTask == '目标检测' or self.mainwindow.projectTask == '实例分割':
            item = self.currentItem()
            if item and self.mainwindow.canvas.editing():
                self.mainwindow._noSelectionSlot = True
                self.mainwindow.canvas.selectShape(self.mainwindow.itemsToShapes[item])
            # shape = self.mainwindow.itemsToShapes[item]

    def currentItem(self):
        items = self.selectedItems()
        if items:
            return items[0]
        return None


    def editLabel(self):
        if self.mainwindow.projectTask == '目标检测' or self.mainwindow.projectTask == '实例分割':
            try:
                if not self.mainwindow.canvas.editing():
                    return
                item = self.currentItem()
                if item == None:
                    return
                else:
                    text = self.mainwindow.labelDialog.popUp(item.text())  ####
                    if text is not None:
                        item.setText(text)
                        item.setBackground(generateColorByText(text))
                        self.mainwindow.setDirty()
            except Exception as e:
                self.mainwindow.logger.log(Logger.ERROR, str(e), exception=True)